package com.lmx.persistence.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.lmx.common.constants.CommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.repoDomain.tmodel.CustomerDrugProject;
import com.lmx.repoDomain.tmodel.CustomerDrugProjectExe;
import com.lmx.repoDomain.tmodel.unpersis.DrugPlanExeDayRecord;
import com.lmx.repoDomain.tmodel.unpersis.DrugPlanExeRecord;

/**
 * 用药计划执行情况统计
 *
 * @author ytche
 * @date 2017年7月27日
 *
 */
@Service(value = "drugPlanExeRecordService")
public class DrugPlanExeRecordServiceImpl implements DrugPlanExeRecordService {
	@Autowired
	private CustomerDrugProjectExeService exeService;
	@Autowired
	private CustomerDrugProjectService projectService;

	@Override
	public Iterable<DrugPlanExeRecord> findAll(Map<String, String> filter) {
		List<DrugPlanExeRecord> records = Lists.newArrayList();
		Long tenantId = ActionContextHolder.getContext().getTenantId();
		if (!filter.containsKey("customer.id")) {
			return records;
		}
		if (!(filter.containsKey("type") && filter.containsKey("statisticTime"))) {
			return records;
		}
		List<CustomerDrugProjectExe> realExes = this.exeService.find(tenantId,
				filter);
		if (realExes == null || realExes.size() < 1) {
			return records;
		}
		// 按用药计划对原始数据分组
		Map<CustomerDrugProject, List<CustomerDrugProjectExe>> exeGroupByProject = realExes
				.stream()
				.collect(
						Collectors
								.groupingBy(CustomerDrugProjectExe::getDrugProject));
		Integer drugProjectNum = 1;// 执行计划计数器
		Integer dayRecordNum = 1;// 每日执行计划记录计数器
		for (Entry<CustomerDrugProject, List<CustomerDrugProjectExe>> customerDrugProject : exeGroupByProject
				.entrySet()) {
			DrugPlanExeRecord record = new DrugPlanExeRecord();
			record.setDayRecords(new ArrayList<DrugPlanExeDayRecord>());
			record.setId(new Long(drugProjectNum));
			this.recordForMonth(record, customerDrugProject,
					filter.get("statisticTime"),
					filter.get("type").equals(CommonConstants.TYPE_OR_MONTH),
					dayRecordNum);
			record.setProject(customerDrugProject.getKey());
			records.add(record);
			drugProjectNum++;
		}
		return records;
	}

	/**
	 * 根据月份统计数据
	 *
	 * @author ytche
	 * @param record
	 * @param customerDrugProjectEntry
	 * @param dateStr
	 * @param isMonth
	 * @param dayRecordNum
	 * @date 2017年7月28日 下午5:20:31
	 */
	private void recordForMonth(
			DrugPlanExeRecord record,
			Entry<CustomerDrugProject, List<CustomerDrugProjectExe>> customerDrugProjectEntry,
			String dateStr, boolean isMonth, Integer dayRecordNum) {
		List<CustomerDrugProjectExe> monthData = customerDrugProjectEntry
				.getValue();
		// 本月或者本周的初始时间
		Integer beginTime = isMonth ? CommonUtil.getMonthBegin(dateStr)
				: CommonUtil.getWeekBegin(dateStr);
		Integer dayCount = isMonth ? 31 : 7;
		for (int i = 0; i < dayCount; i++) {
			Integer startTime = beginTime + 3600 * 24 * i;
			Integer endTime = startTime + 3600 * 24;
			List<CustomerDrugProjectExe> dayDrugExes = monthData
					.stream()
					.filter(p -> p.getCreateDateTime() >= startTime
							&& p.getCreateDateTime() < endTime)
					.collect(Collectors.toList());

			DrugPlanExeDayRecord dayRecord = new DrugPlanExeDayRecord();
			dayRecord.setId(dayRecordNum.longValue());
			dayRecord.setDate(i + 1);
			if (!isMonth) {
				dayRecord.setDay(i);
			}
			dayRecord.setExecutions(dayDrugExes);
			record.getDayRecords().add(dayRecord);
			dayRecordNum++;
		}

	}
}
